package com.sunxueqi.framework.web.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.sunxueqi.common.exception.BaseException;
import com.sunxueqi.common.exception.CustomException;
import com.sunxueqi.framework.web.vo.ResponseData;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 基础异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(BaseException.class)
	public ResponseData baseException(BaseException e) {
		return ResponseData.error(e.getMessage());
	}
	
	/**
	 * 业务异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(CustomException .class)
	public ResponseData businessException(CustomException e) {
		if(StrUtil.isEmptyIfStr(e.getCode())) {
			return ResponseData.error(e.getMessage());
		}
		return ResponseData.error(e.getCode(), e.getMessage());
	}
	
	
	
}
